package com.transpeed.payservice.pay.unionpay;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.json.JSONObject;

import com.transpeed.payservice.Utils.JsonUtils;
import com.transpeed.payservice.Utils.MD5Utils;

public class RefundUmsPayTrade {
	public static final String ADD_URL = "http://umspay.izhong.me/netpay-route-server/api/";

	public static void test_trade_Refund(String billDate , String billNo , int refundAmount ) {
		try {
			// 创建连接
			URL url = new URL(ADD_URL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			connection.connect();
			// POST请求
			SimpleDateFormat requestTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());

			String RequestTimestamp = requestTimeFormat.format(new Date());


			HashMap<String, Object> test = new HashMap<String, Object>();

			test.put("msgId", "800000000010");
			test.put("msgSrc", "WWW.TRANSPEEDSG.COM");
			test.put("msgType", "bills.refund");
			test.put("requestTimestamp", RequestTimestamp);
			test.put("srcReserve", "1234");
			test.put("billDate", billDate);
			test.put("billNo", billNo);
			test.put("instMid", "QRPAYDEFAULT");
			test.put("mid", "911000000000000");
			test.put("tid", "88880001");
			
			
			test.put("refundAmount", refundAmount);
			test.put("refundDesc", "苏E88888");

			
			// MD5Utils.getLexicographicalOrder(test,"DSANEdAEJCDNTDaS7ye8jFfBSzNGTZkR5SFzHfn8TWkSd5fK");
			test.put("sign", MD5Utils.getSignature(test,"DSANEdAEJCDNTDaS7ye8jFfBSzNGTZkR5SFzHfn8TWkSd5fK"));

			System.out.println("发送Json为 \n" + JsonUtils.formatAsJSON(new JSONObject(test).toString()));
			byte [] bytes =  new JSONObject(test).toString().getBytes("UTF-8");
			out.write(bytes);
			out.flush();
			out.close();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "gbk");
				sb.append(lines);
			}

			reader.close();
			// 断开连接
			connection.disconnect();

			System.out.println("接收Json为 \n" + JsonUtils.formatAsJSON(sb.toString()));

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		test_trade_Refund("2017-04-11","307514918981362941761911",2);
	}
}
